﻿using System;
using System.Collections.Generic;
using System.Linq;
using Outlook.WorkItems.Extension.Manager.TeamProjectManager;
using Microsoft.Office.Interop.Outlook;
using System.Xml.Linq;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Outlook.WorkItems.Extension.Manager.TeamCalendarManager
{
    public class SynchronizeWorkItemsAction : IAction<MAPIFolder>
    {
        public WorkItemCalendarLink ParentLink { get; set; }
        public List<MappingField> MappingFields { get; set; }
        public string QueryName { get; set; }

        public SynchronizeWorkItemsAction()
        {
            MappingFields = new List<MappingField>();
            QueryName = "";
        }

        public void DeserializeAction(XElement action)
        {

            QueryName = action.Attribute("queryName").Value;
            MappingFields = (from mf in action.Element("MappingFields").Elements()
                                  select new MappingField()
                                  {
                                      WiFieldName = mf.Attribute("wiField").Value,
                                      MessageFieldName = mf.Attribute("messageField").Value,
                                  }).ToList();
        }

        public void DoAction(MAPIFolder item)
        {
            if (item == null || item.DefaultItemType != OlItemType.olAppointmentItem)
                throw new ArgumentException("item must be a MAPIFolder Type and DefaultItemType must be olAppointmentItem");

            var tfs = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(ParentLink.TeamServerUri));
        }

        public XNode SerializeAction()
        {
            return new XElement("SynchronizeWorkItemsAction",
                       new XAttribute("queryName", QueryName == null ? "" : QueryName),
                       new XElement("MappingFields",
                           from mf in this.MappingFields
                           select new XElement("MappingField",
                                       new XAttribute("wiField", mf.WiFieldName),
                                       new XAttribute("messageField", mf.MessageFieldName))));
        }
    }
}
